var form, select, parentMenuSelect;
var menuId = getTempData('updateMenuId');
// 当前选中的菜单类型
var menuType = 0;

$(function () {
    initLayUI(function () {
        initData();
        initButtons();
    });
});

function initLayUI(cb) {
    layui.use(['form', 'xmSelect'], function () {
        form = layui.form;
        select = layui.xmSelect;

        form.render();

        form.verify({
            perms: function (value) {
                if (menuType > 1) {
                    if (value.length === 0) {
                        return "必填项不能为空";
                    }
                }
            },
            url: function (value) {
                if (menuType === 1) {
                    if (value.length === 0) {
                        return "必填项不能为空";
                    }
                }
            }
        });

        form.on('submit(menuEditForm)', function (data) {
            let menuType = parseInt(data.field.menuType);
            let parentMenuId = parentMenuSelect.getValue('value');
            if (parentMenuId.length === 0) {
                layerUtil.error("请选择上级菜单");
                return false;
            }
            let menu = {
                "menuName": data.field.menuName,
                "orderNum": data.field.orderNum,
                "menuType": menuType,
                "icon": data.field.icon,
                "parentId": parentMenuId[0],
            };
            if (menuType === 1) {
                menu.url = data.field.url;
                menu.openType = data.field.openType;
                menu.perms = '';
            } else if (menuType === 2) {
                menu.url = '';
                menu.perms = data.field.perms;
                menu.openType = -1;
            } else {
                menu.perms = '';
                menu.url = '';
                menu.openType = -1;
            }
            if (menuId !== undefined) {
                menu.menuId = menuId;
                updateMenu(menu);
            } else {
                addMenu(menu);
            }
            return false;
        });

        form.on('radio(menuType)', function (data) {
            menuType = parseInt(data.value);
            changeDisplay(menuType);
            form.render();
        });

        cb();
    });
}

function initButtons() {
    $("#cancelBtn").click(function () {
        layerUtil.closeLayer(parent.window.iframeIndex);
    });
}

function initData() {
    // 后续有时间可以替换成Promise
    initMenuType(function () {
        initParentMenuData(function () {
            initMenuData();
        });
    });
}

function initMenuType(cb) {
    ajaxUtil.loadDict("sys_menu_type,sys_menu_open_type", function (data) {
        data.forEach(function (item) {
            if (item.dictType === 'sys_menu_type') {
                let radioStr = "<input type='radio' name='menuType' lay-filter='menuType' value='" + item.dictValue + "'";
                if (item.isDefault) {
                    radioStr += " checked ";
                }
                radioStr += " title='" + item.dictLabel + "'/>";
                $("#menuType").append(radioStr);
            } else {
                let radioStr = "<input type='radio' name='openType' lay-filter='openType' value='" + item.dictValue + "'";
                if (item.isDefault) {
                    radioStr += " checked ";
                }
                radioStr += " title='" + item.dictLabel + "'/>";
                $("#openType").append(radioStr);
            }
        });

        form.render();

        cb();
    });
}

function initParentMenuData(cb) {
    ajaxUtil.simpleGet("/menu/queryMenuTree", function (data) {
        let menuData = [{name: '根目录', value: -1, children: data}];
        parentMenuSelect = select.render({
            el: '#parentMenuId',
            radio: true,
            toolbar: {
                show: true,
                list: ['ALL', 'REVERSE', 'CLEAR']
            },
            tree: {
                show: true,
                showFolderIcon: true,
                strict: false,
            },
            data: menuData
        });

        cb();
    });
}

function initMenuData() {
    if (menuId !== undefined) {
        ajaxUtil.simpleGet("/menu/queryMenuById?menuId=" + menuId, function (data) {
            form.val('form', {
                "menuName": data.menuName,
                "orderNum": data.orderNum,
                "menuType": data.menuType,
                "openType": data.openType,
                "icon": data.icon,
                "perms": data.perms,
                "url": data.url
            });
            changeDisplay(data.menuType);
            let parentArr = [data.parentId];
            // 多选选中
            parentMenuSelect.setValue(parentArr);
        });
    }
}

function changeDisplay(value) {
    if (value === 2) {
        $("#permsDiv").show();
        $("#openTypeDiv").hide();
        $("#iconDiv").hide();
    } else if (value === 1) {
        $("#permsDiv").hide();
        $("#urlDiv").show();
        $("#openTypeDiv").show();
        $("#iconDiv").show();
    } else {
        $("#permsDiv").hide();
        $("#urlDiv").hide();
        $("#openTypeDiv").hide();
        $("#iconDiv").show();
    }
}

function addMenu(menu) {
    ajaxUtil.simplePost(menu, "/menu/add", function () {
        layerUtil.closeLayer();
        reloadParentTable('menuTable');
    });
}

function updateMenu(menu) {
    ajaxUtil.simplePost(menu, "/menu/update", function () {
        layerUtil.closeLayer();
        reloadParentTable('menuTable');
    });
}